#include"Config.h"
using namespace std;
map<string,string> Config::dict;
void Config::load(string filename){
  ifstream fin;
  fin.open(filename.c_str());
  assert(fin.is_open());
  string line;
  while(getline(fin,line)){
    if(line[0]=='#')continue;
    istringstream cins(line);
    string key,value,temp;
    cins>>key;
    cins>>temp;
    cins>>value;
    Config::dict.insert(pair<string,string>(key,value));
  }
}
double Config::getdouble(string key){
    map<string,string>::iterator ite=Config::dict.find(key);
    if(ite!=Config::dict.end()){
      string value=ite->second;
      return atof(value.c_str());
    }else{
      cerr<<"error: the value of "<<key<<" does not exist in config file!"<<endl;
      return 0;
    }
}
string Config::getstring(string key){
    map<string,string>::iterator ite=Config::dict.find(key);
    if(ite!=Config::dict.end()){
      return ite->second;
    }else{
      cerr<<"error: the value of "<<key<<" does not exist in config file!"<<endl;
      return "";
    }
}
bool Config::getbool(string key){
    string value=Config::getstring(key);
    if(!value.empty()){
      if(value[0]=='t'||value[0]=='T')return true;
    }
    return false;
}

int Config::getint(string key){
    string value=Config::getstring(key);
    if(!value.empty())return atoi(value.c_str());
    return 1;
}
  
